<template>
	<div class="" style="width: 100%;">
		<head-top></head-top>

	
		<div class="table_container" :style="'width: 90%;  overflow: hidden;  height:'+(winHeight/1.2)+'px;'">
			<div style="margin: 5px; display: flex;">
				<span style="font-size: 20px;width: 300px;">申购赎回 (账户维度):  </span>
				<div>
					<el-button size="mini" type="success" @click="refleash()">刷新</el-button>
					<el-button size="mini" type="primary" @click="addnew_arrange()">新增</el-button>
				</div>

				<span style="font-size: medium; padding: 10px;">账户:</span>
				<el-select v-model="account_choice" size="small" clearable filterable placeholder="请选择" 
					@change="changeaccount_choice()">
					<el-option v-for="item in account_options" :key="item.label" :label="item.label"
						:value="item.value">
					</el-option>
				</el-select>

				<span style="font-size: medium; padding: 10px;">日期:</span>
				<el-select v-model="date_choice" size="small" clearable filterable placeholder="请选择"
					@change="changedate_choice()">
					<el-option v-for="item in date_options" :key="item.label" :label="item.label" :value="item.value">
					</el-option>
				</el-select>

				<div style="display: flex; flex-flow: row; width: 300px;padding-left: 100px;">
						往前看:  <el-input  style=" width: 100px;" v-model="inputdatenum" size="small" ></el-input>  条
				</div>
				
				<span style="padding-left:30px;">按完成排序:</span>
				<el-switch style="margin-left: 30px;height: 30px; "
				  v-model="order_flag"
				  active-color="#13ce66"
				  inactive-color="#ff4949">
				</el-switch>
				
				<!-- <div>
					<el-button  style="margin-left: 15px;" size="mini" type="warning" @click="finish_all()">所有已处理</el-button>
					<el-button  style="margin-left: 15px;" size="mini" type="success" @click="finish_today()">当日已处理</el-button>
					<el-button  style="margin-left: 15px;" size="mini" type="primary" @click="unfinish_today()">当日重置</el-button>
				</div> -->
			</div>

			<el-table ref="singleTable" :data="arrange_data" height="1100px"  border size='small'
				:style="'width:'+(screenWidth*1.0)+'px; ;' " style="overflow-y: scroll;overflow-x: scroll;" :row-class-name=" tableRowClassNamemoneyout">
				<el-table-column prop="id" label="id" width="100" align></el-table-column>
				
				<el-table-column prop="kkdate" label="date" width="120"   align>
					<template slot-scope="scope">
						<el-input  v-if="scope.row['status'].indexOf('完成') == -1"  v-model="scope.row['kkdate']" 
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-if="scope.row['status'].indexOf('完成') != -1" > {{scope.row['kkdate'] }}</span> 
					</template>
					
				</el-table-column>
	
				<el-table-column prop="program_code" label="program_code" width="200"  sortable="" align>
					<template slot-scope="scope">
						<el-select  v-if="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['program_code']" size="small" allow-create clearable filterable
							 style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in account_options" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
						<span  v-if="scope.row['status'].indexOf('完成') != -1" > {{scope.row['program_code'] }}</span> 
					</template>
				</el-table-column>
				
				
				<el-table-column prop="action_type" label="action_type" width="140"   align>
					<template slot-scope="scope">
						<el-select  v-if="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['action_type']" size="small"clearable filterable allow-create
						   collapse-tags style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in direction_options" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					     <span  v-if="scope.row['status'].indexOf('完成') != -1" > {{scope.row['action_type'] }}</span> 
					</template>
				   
				</el-table-column>
				
			
			
			
				
						
			
				<el-table-column prop="chg_amount_netvalue" label="chg_amount_netvalue" width="140" align>
					<template slot-scope="scope">
						<el-input  v-if="scope.row['status'].indexOf('完成') == -1"  v-model="scope.row['chg_amount_netvalue']"
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-if="scope.row['status'].indexOf('完成') != -1" > {{scope.row['chg_amount_netvalue'] }}</span> 
					</template>
					
				</el-table-column>
				<el-table-column prop="chg_amount_netvalue" label="amount_check" width="140" align>
					<template slot-scope="scope">
						
						<span > {{formatNumber(scope.row['chg_amount_netvalue'])}}万</span> 
					</template>
					
				</el-table-column>
			
				<!-- <el-table-column prop="chg_amount_cta" label="chg_amount_cta" width="140" align>
					<template slot-scope="scope">
						<el-input  v-if="scope.row['status'].indexOf('完成') == -1"  v-model="scope.row['chg_amount_cta']"
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-if="scope.row['status'].indexOf('完成') != -1" > {{scope.row['chg_amount_cta'] }}</span> 
					</template>
					
				</el-table-column>
				
				<el-table-column prop="chg_to_today_portion" label="chg_to_today_portion" width="140" align>
					<template slot-scope="scope">
						<el-select  v-if="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['chg_to_today_portion']" size="small"  
						   collapse-tags style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in yseno_options" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
						<span  v-if="scope.row['status'].indexOf('完成') != -1" > {{scope.row['chg_to_today_portion'] }}</span> 
					</template>
					
				</el-table-column>
				<el-table-column prop="already_in_qry" label="already_in_qry" width="140" align>
					<template slot-scope="scope">
						<el-select  v-if="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['already_in_qry']" size="small"  
						   collapse-tags style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in yseno_options" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
						<span  v-if="scope.row['status'].indexOf('完成') != -1" > {{scope.row['already_in_qry'] }}</span> 
					</template>
					
				</el-table-column> -->
				<el-table-column prop="future_in_transit" label="future_in_transit" align>
					<template slot-scope="scope">
						<el-input  v-if="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['future_in_transit']" 
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-if="scope.row['status'].indexOf('完成') != -1" > {{scope.row['future_in_transit'] }}</span> 
					</template>
					
				</el-table-column>
				
				<el-table-column prop="note" label="备注" align>
					<template slot-scope="scope">
						<el-input  v-if="scope.row['status'].indexOf('完成') == -1" v-model="scope.row['note']" 
							size="small" placeholder="" @change=" change_cell($event,scope)"></el-input>
						<span  v-if="scope.row['status'].indexOf('完成') != -1" > {{scope.row['note'] }}</span> 
					</template>
					
				</el-table-column>
				



				<!-- <el-table-column prop="kkupdatetime" label="更新时间" width="140" align></el-table-column> -->
				<el-table-column prop="status" label="状态" width="120" align>
					<template slot-scope="scope">
						<!-- :disabled="scope.row['status']=='完成' " -->
						<el-select  v-model="scope.row['status']" size="small" collapse-tags style="" placeholder="请选择"
							@change=" change_cell($event,scope)">
							<el-option v-for="item in status_options" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					
					</template>
				</el-table-column>
				<el-table-column label="操作" :width='90'>
					<template slot-scope="scope">
						<el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除
						</el-button>
						<!-- <el-button size="small" type="warning" @click="index(scope.$index, scope.row)">详情</el-button> -->
					</template>
				</el-table-column>
			</el-table>

		</div>
	
	</div>
</template>

<script>
	import headTop from '@/components/headTop'
	export default {
		data() {
			return {
				checkflag: false,
				core_pos: [],
				pos_index_item: '',
				account_choice: "",
				account_options: [],
				target_pos_index_options: [],
				date_choice: "",
				date_options: [],
				tableData: [],
				currentRow: null,
				inputdatenum:120,
				
				
				count: 0,
				exchange_detail: [],
				LP_account_list: [],
			
				fix_account: {},
				visible: false,
				visible2: false,
				order_flag:true,

				screenWidth: document.body.clientWidth, // 屏幕尺寸
				screenHeight: document.body.clientHeight, // 屏幕尺寸
				purchase_id: "",
				arrange_data: [],

			
				worker_list:[
					{
						value: '朱秀鹏',
						label: '朱秀鹏'
					},
					
					{
						value: '游雄凯',
						label: '游雄凯'
					},
					{
						value: '胡睦',
						label: '胡睦'
					},
					{
						value: '郭健',
						label: '郭健'
					},
					{
						value: '张边远',
						label: '张边远'
					},
					{
						value: '安姣姣',
						label: '安姣姣'
					},
					{
						value: '刘禹',
						label: '刘禹'
					},
					
				],
				status_options: [{
						value: '待处理',
						label: '待处理'
					},
					{
						value: '未出金',
						label: '未出金'
					},
					{
						value: '异常',
						label: '异常'
					},
					{
						value: '完成',
						label: '完成'
					},
					{
						value: '准备',
						label: '准备'
					},
					{
						value: '跟踪中',
						label: '跟踪中'
					},
				],
				yseno_options: [{
						value: 'yes',
						label: 'yes'
					},
					{
						value: 'no',
						label: 'no'
					},
					
				
				],
				
				direction_options: [
					// {
					// 	value: 'purchase',
					// 	label: 'purchase'
					// },
					{
						value: 'redeem',
						label: 'redeem'
					},
					
					{
						value: 'cashin',
						label: 'cashin'
					},
					
					{
						value: 'adjust',
						label: 'adjust'
					},
					{
						value: 'transfer',
						label: 'transfer'
					},
					
					
				
				],
				

			}
		},
		components: {
			headTop,
		},



		activated() {

			// console.log("当前登入用户", this.$store.state.adminInfo.name);
			//    if (this.tradesysPermission_list.indexOf (this.$store.state.adminInfo.permission) ==-1) {
			// 	console.log(this.$store.state.adminInfo.name,"未登入")
			// 	this.$message({type: 'error',	message: '没有权限',});
			// 	this.$router.push({
			// 		path: `/manage`
			// 	})
			// }
		},
		mounted() {
			this.get_exchange_arrange()
		},
		created() {
			this.get_tradesys_account()
			
			this.get_date_last()
		},
		computed: {
			
		},

		methods: {
			formatNumber(num) {  
			    // 检查输入是否为数字
				  
			    if (typeof num !== 'number' || isNaN(num)) {  
			        return '输入无效';  
			    }  
			    num=num/10000
			    // 判断数字是否为整数（使用取余运算）  
			    if (Number.isInteger(num) || (num % 1 === 0)) {  
			        // 如果是整数，直接返回整数部分（转为字符串以去除科学计数法表示）  
			        return num.toString();  
			    } else {  
			        // 如果不是整数，保留两位小数并返回字符串形式  
			        return num.toFixed(2);  
			    }  
			} ,
			
			changedate_choice() {

				this.get_exchange_arrange()
			},

			changeaccount_choice() {
				this.get_exchange_arrange()
			},
			change_cell($event, scope) {
				// console.log(scope.$index)
				var that = this
				console.log(scope)
				var arrange_data_item= this.arrange_data[scope.$index]
				
				if (!isNaN(parseFloat(arrange_data_item['chg_amount_netvalue']))) {
					console.log(parseFloat(arrange_data_item['chg_amount_netvalue']))
					if (arrange_data_item['action_type']=='purchase' && parseFloat(arrange_data_item['chg_amount_netvalue'])<=0 ){
						this.$message({
							message: 'purchase chg_amount_netvalue 必须大于0',
							type: 'warning'
						});
						return
					}
					if (arrange_data_item['action_type']=='cashin' && parseFloat(arrange_data_item['chg_amount_netvalue'])<=0 ){
						this.$message({
							message: 'cashin chg_amount_netvalue 必须大于0',
							type: 'warning'
						});
						return
					}
					if (arrange_data_item['action_type']=='redeem' && parseFloat(arrange_data_item['chg_amount_netvalue'])>0 ){
						this.$message({
							message: 'redeem chg_amount_netvalue 必须小于0',
							type: 'warning'
						});
						return
					}
								
					
				}
				
				
				let sql2 = "UPDATE `prod_purchase_redeem` SET `action_type` = '" + arrange_data_item['action_type'] + "',   \
				   `date` = '" + arrange_data_item['kkdate'] + "', `program_code` = '" + arrange_data_item['program_code'] + "',   \
					`chg_amount_netvalue` = '" + arrange_data_item['chg_amount_netvalue'] + "', `chg_amount_cta` = '" + arrange_data_item['chg_amount_cta'] + "',   \
					`chg_to_today_portion` = '" + arrange_data_item['chg_to_today_portion'] + "', `already_in_qry` = '" + arrange_data_item['already_in_qry'] + "', \
					 `future_in_transit` = '" + arrange_data_item['future_in_transit'] + "', \
					`note` = '" + arrange_data_item['note'] + "', `status` = '" + arrange_data_item['status'] + "'\
					WHERE `id` = " + arrange_data_item['id'] + "  ;"

				that.$axios.post(that.lpapi2, "form=do_sql&sql=" + sql2).then(response => {
					console.log(response, 'do_sql');
					if (response.status = 200) {
						this.$message({
							message: '更新',
							type: 'success'
						});
						// this.get_exchange_arrange()
					} else {
						return
						console.log(response.data.statusCode);
					}
				})






			},
			
			

			
			
			handleDelete(index, row) {

				console.log("row", row['kkdate'],"program_code",row['program_code'])
				this.purchase_id = row['id']
				
				
				// this.exchange_arrange=row
				var that = this;

				this.$confirm('此操作将永久修改数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',inputType: 'password',
					type: 'warning'
				}).then(() => {


					let sql2 = "delete from `prod_purchase_redeem`  WHERE `id`='" + this.purchase_id + "'   ;"

					that.$axios.post(that.lpapi2, "form=do_sql&sql=" + sql2).then(response => {
						console.log(response, 'do_sql');
						if (response.status = 200) {
							this.$message({
								message: '删除完成',
								type: 'success'
							});
							this.get_exchange_arrange()
						} else {
							return
							console.log(response.data.statusCode);
						}
					})


				}).catch(() => {
					this.$message({
						type: 'info',
						message: '取消删除'
					});
				});
			},

		
		
		
		
			refleash() {
				this.get_exchange_arrange()
			},
			addnew_arrange() {
				var that = this;
				var date = new Date();
				var seperator1 = "-";
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var strDate = date.getDate();
				if (month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if (strDate >= 0 && strDate <= 9) {
					strDate = "0" + strDate;
				}
				

				var currentdate = year + seperator1 + month + seperator1 + strDate;
				
				
				
				
				
				let sql2 = "INSERT INTO `prod_purchase_redeem`(`date`,`status`,`updatetime`,`action_type`,`future_in_transit`,`chg_amount_netvalue`,`chg_amount_cta`,`chg_to_today_portion`,`already_in_qry` \
				,`program_code`) VALUES ('" +currentdate + "','准备',CURRENT_TIMESTAMP,'purchase','','0','0','no','no','');"
				that.$axios.post(that.lpapi2, "form=do_sql&sql=" + sql2).then(response => {
					console.log(response, 'do_sql');
					if (response.status = 200) {
						this.$message({
							message: '新增完成',
							type: 'success'
						});
						this.get_exchange_arrange()
					} else {
						return
						console.log(response.data.statusCode);
					}
				})

			},

			get_date_last() {
				var that = this;
				
				
				let sql =" select date_format(date,'%Y-%m-%d') as kkdate ,date from `prod_purchase_redeem`  GROUP BY date;"
				that.$axios.post(that.lpapi2, "form=get_data&sql=" + sql).then(response => {
					console.log(response, 'data_tradesys_account');
					if (response.status = 200) {
						that.date_options = []
						//for (var i = 0; i < response.data.length; i++) {
						for (var i = response.data.length - 1; i >= 0; i--) {
							let char = {
								value: response.data[i]['kkdate'],
								label: response.data[i]['kkdate'],
							};
							that.date_options.push(char);
						}
					} else {
						console.log(response.data.statusCode);
					}
				})

			},
			finish_all(){
				
				var that = this;
				
				
				let sql2 = "update `prod_purchase_redeem` set status='完成' "
				that.$axios.post(that.lpapi2, "form=do_sql&sql=" + sql2).then(response => {
					console.log(response, 'do_sql');
					if (response.status = 200) {
						this.$message({
							message: '更新完成',
							type: 'success'
						});
						this.get_exchange_arrange()
					} else {
						return
						console.log(response.data.statusCode);
					}
				})
				
			},
			unfinish_today(){
				if (this.date_choice==''){
					this.$message({
						message: '日期未选择',
						type: 'warning'
					});
					return
				}
				var that = this;
				let sql2 = "update `prod_purchase_redeem` set status='准备' where date='"+this.date_choice+ "';"
				that.$axios.post(that.lpapi2, "form=do_sql&sql=" + sql2).then(response => {
					console.log(response, 'do_sql');
					if (response.status = 200) {
						this.$message({
							message: '更新完成',
							type: 'success'
						});
						this.get_exchange_arrange()
					} else {
						return
						console.log(response.data.statusCode);
					}
				})
				
			},
			finish_today(){
				
				var that = this;
				
				
				let sql2 = "update `prod_purchase_redeem` set status='完成' where date=CURRENT_DATE();"
				that.$axios.post(that.lpapi2, "form=do_sql&sql=" + sql2).then(response => {
					console.log(response, 'do_sql');
					if (response.status = 200) {
						this.$message({
							message: '更新完成',
							type: 'success'
						});
						this.get_exchange_arrange()
					} else {
						return
						console.log(response.data.statusCode);
					}
				})
				
			},

			get_tradesys_account() {
				var that = this;
				// console.log(that.lpapi2)

				let sql = "select distinct program_code from config_account where use_flag='yes' ;"
				that.$axios.post(that.lpapi2, "form=get_data&sql=" + sql).then(response => {

					console.log(response, 'cconfig_product')
					let plat_list = []
					if (response.status = 200) {
						that.account_options = []
						that.LP_account_list = response.data;
						for (var i = 0; i < response.data.length; i++) {
							let char = {
								value: response.data[i]['program_code'],
								// label: response.data[i]['account'],
								label: response.data[i]['program_code'],

							};

							that.account_options.push(char);
						}

					} else {
						console.log(response.data.statusCode);
					}
				})


			},
			get_exchange_arrange() {
				var that = this;
				console.log(that.lpapi2)
				// let flag1='入金'
				// if (this.inoutflag){
				// 	 flag1='出金'
				// }else{
				// 	 flag1='入金'
					
				// }
				let order_str=""
				if (this.order_flag){
					
					order_str="CASE WHEN status = '完成' THEN 1  ELSE 0    END ASC,"
				}
				else {
					
					order_str="date desc,program_code,"
				}
				let sql =
					"select * ,date_format(date,'%Y-%m-%d') as kkdate,date_format(updatetime,'%Y-%m-%d  %H:%i:%s') as kkupdatetime \
					from `prod_purchase_redeem`   ORDER BY "+order_str+" id desc  limit "+this.inputdatenum+";"
			
				if (this.account_choice != "" && this.date_choice != "") {
					sql =
						"select * ,date_format(date,'%Y-%m-%d') as kkdate,date_format(updatetime,'%Y-%m-%d  %H:%i:%s') as kkupdatetime \
					from `prod_purchase_redeem` where program_code='" + this.account_choice + "' and  date='" + this.date_choice +
						"' ORDER BY "+order_str+" id desc  limit "+this.inputdatenum+";"
				} else if (this.account_choice != "") {
					sql =
						"select * ,date_format(date,'%Y-%m-%d') as kkdate,date_format(updatetime,'%Y-%m-%d  %H:%i:%s') as kkupdatetime \
					from `prod_purchase_redeem` where   program_code='" + this.account_choice +
						"'  ORDER BY "+order_str+" id desc  limit "+this.inputdatenum+";"
				} else if (this.date_choice != "") {
					sql ="select * ,date_format(date,'%Y-%m-%d') as kkdate,date_format(updatetime,'%Y-%m-%d  %H:%i:%s') as kkupdatetime \
					from `prod_purchase_redeem` where  date='" + this.date_choice + "'  ORDER BY  "+order_str+" id desc  limit "+this.inputdatenum+";"
				}
				
				
				
				
				that.arrange_data = []
				that.$axios.post(that.lpapi2, "form=get_data&sql=" + sql).then(response => {

					console.log(response.data[0], 'prod_purchase_redeem')

					if (response.status = 200) {
						if (response.data[0].hasOwnProperty('message')) {
							that.arrange_data = []
						} else {
							that.arrange_data = response.data;
						}
						// that.arrange_data = response.data;
					} else {
						console.log(response.data.statusCode);
					}
				})


			},




			tableRowClassNamemoneyout({
				row,
				rowIndex
			}) {
		
				if (this.arrange_data[rowIndex]['status'] == '待处理' | this.arrange_data[rowIndex]['status'] == '准备' ) {
					return 'bacorange';
				}
				if (this.arrange_data[rowIndex]['status'] == '未出金'  ) {
					return 'DarkGray';
				}
				if (this.arrange_data[rowIndex]['status'] == '跟踪中' ) {
					return 'LightCoral';
				}
				if (this.arrange_data[rowIndex]['status'] == '异常') {
					return 'bacOrangeRed';
				}
				return '';
			}

		},
	}
</script>

<style>
	.el-table .bacOrangeRed {
		background: OrangeRed;
	}

	.el-table .bacorange {
		background: orange;
	}

	.el-table .success-row {
		background: grey;
		color: white;
	}

	.table_container {
		margin: 20px 5%;
		/* background-color: #0000FF; */
	}

	.el-table .grey {
		background: Silver;
	}

	.el-table .DarkGray {
		background: DarkGray;
	}

	.el-table .LightCoral {
		background: LightCoral;
		color: white;
	}

	.el-table .LimeGreen {
		background: LimeGreen;
	}

	.el-table .green {
		background: green;
	}

	.el-table .LightSlateGray {
		background: LightSlateGray;
	}

	.el-table .red {
		background: red;
		color: white;
	}

	.el-table .orange {
		background: NavajoWhite;
		color: black;
	}

	.el-table .blue {
		background: blue;
	}

	.el-table .purple {
		background: Violet;
		color: black;
	}

	.el-table .LightSteelBlue {
		background: LightSteelBlue;
	}

	.el-table .Goldenrod {
		background: Goldenrod;
	}

	.el-table .xingmu {
		background: Goldenrod;
		color: red;
		font-weight: bold;

	}


	.el-table .black {
		background: black;
		color: white;
	}

	.el-table .green {
		background: green;
		color: white;
	}

	.el-table .CornflowerBlue {
		background: CornflowerBlue;
		color: white;
	}

	.el-table .DodgerBlue {
		background: DodgerBlue;
	}
</style>
